package mtn.bowen.yan.server.dao;

import mtn.bowen.yan.server.model.PollsSuggest;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: TODO
 * @date 2021-11-04 7:52 AM
 */
@Mapper
@Repository
public interface PollsSuggestDao {

    String ID = "id";

    String TABLE_NAME = "polls_suggest";

    String ALL_FIELDS = "id,uuid,kind,name,chef,create_time,suggest,state";

    String UNIQUE_FIELDS = "`state`, `uuid`, `create_time`";

    String OTHER_FIELDS = "`kind`, `name`, `chef`, `suggest`";

    @Select("select " + ALL_FIELDS + " from " + TABLE_NAME)
    List<PollsSuggest> selectAll();

    @Insert("INSERT INTO " + TABLE_NAME + " (" + UNIQUE_FIELDS + ", " + OTHER_FIELDS + ") VALUES (#{state}, #{uuid}, #{createTime}, #{kind}, #{name}, #{chef}, #{suggest})" + " ON DUPLICATE KEY UPDATE `state`=#{state}, `uuid`=#{uuid}, `create_time`=#{createTime}, `kind`=#{kind}, `name`=#{name}, `chef`=#{chef}, `suggest`=#{suggest}")
    @Options(useGeneratedKeys = true, keyColumn = ID)
    int insertOnUpdate(PollsSuggest pollsSuggest);

    @Update("TRUNCATE TABLE " + TABLE_NAME)
    void truncate();

    @Select("SELECT " + ALL_FIELDS + " FROM " + TABLE_NAME + " WHERE DATE_SUB(CURDATE(), INTERVAL #{day} DAY) <= date(create_time)")
    List<PollsSuggest> selectBeforeSevenDay(@Param("day") Integer day);
}
